/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.ecommercer.dao;

import br.com.ecommercer.bean.Endereco;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Petterson
 */
public class EnderecoDaoImp implements EnderecoDao {

    @Override
    public Object salvar(Object obj) throws Exception {
        Endereco endereco = (Endereco) obj;
        PreparedStatement ps = null;
        Connection conn = null;

        try {
            String SQL = "INSERT INTO endereco(local, rua, bairro, cidade, estado,cep, capacidadeMaxima) VALUES(?,?,?,?,?,?,?)";
            conn = FabricaConexao.getConnection();
            ps = conn.prepareStatement(SQL);

            ps.setString(1, endereco.getLocal());
            ps.setString(2, endereco.getRua());
            ps.setString(3, endereco.getBairro());
            ps.setString(4, endereco.getCidade());
            ps.setString(5, endereco.getEstado());
            ps.setString(6, endereco.getCep());
            ps.setInt(7, endereco.getCapacidadeMaxima());
            ps.executeUpdate();

        } catch (SQLException ex) {
            throw new Exception("Erro ao inserir endereco" + ex.getMessage());
        } finally {
            FabricaConexao.closeConnection(conn, ps);
        }
        return endereco;
    }

    @Override
    public void alterar(Object obj) throws Exception {
        Endereco endereco = (Endereco) obj;
        PreparedStatement ps = null;
        Connection conn = null;

        try {
            String SQL = "UPDATE endereco SET local = ?, rua = ?, bairro = ?, "
                    + "cidade = ?, estado = ? , cep = ?, capacidadeMaxima = ? WHERE id = ?";
            conn = FabricaConexao.getConnection();
            ps = conn.prepareStatement(SQL);

            ps.setString(1, endereco.getLocal());
            ps.setString(2, endereco.getRua());
            ps.setString(3, endereco.getBairro());
            ps.setString(4, endereco.getCidade());
            ps.setString(5, endereco.getEstado());
            ps.setString(6, endereco.getCep());
            ps.setInt(7, endereco.getCapacidadeMaxima());
            ps.setInt(8, endereco.getId());

            ps.executeUpdate();
        } catch (SQLException ex) {
            throw new Exception("Erro ao alterar endereco" + ex.getMessage());
        } finally {
            FabricaConexao.closeConnection(conn, ps);
        }
    }

    @Override
    public void excluir(int id) throws Exception {
        PreparedStatement ps = null;
        Connection conn = null;
        try {
            String SQL = "DELETE FROM endereco WHERE id = ?";
            conn = FabricaConexao.getConnection();
            ps = conn.prepareStatement(SQL);
            ps.setInt(1, id);
            ps.executeUpdate();

        } catch (Exception ex) {
            throw new Exception("Erro ao excluir endereco" + ex.getMessage());
        }finally {
            FabricaConexao.closeConnection(conn, ps);
        }
    }

    @Override
    public Object procurar(Integer id) throws Exception {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs = null;
        Endereco endereco = null;
        try {
            String SQL = "SELECT * FROM endereco WHERE id = ?";
            conn = FabricaConexao.getConnection();
            ps = conn.prepareStatement(SQL);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                endereco = new Endereco();
                endereco.setId(rs.getInt("id"));
                endereco.setLocal(rs.getString("local"));
                endereco.setRua(rs.getString("rua"));
                endereco.setBairro(rs.getString("bairro"));
                endereco.setCidade(rs.getString("cidade"));
                endereco.setEstado(rs.getString("estado"));
                endereco.setCep(rs.getString("cep"));
                endereco.setCapacidadeMaxima(rs.getInt("capacidadeMaxima"));
            }
        } catch (Exception ex) {
            throw new Exception("Erro ao procurar endereco" + ex.getMessage());
        }finally {
            FabricaConexao.closeConnection(conn, ps);
        }
        return endereco;
    }

    @Override
    public List<Object> listar() throws Exception {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs;
        Endereco endereco = null;
        List enderecos = new ArrayList();
        try {
            String SQL = "SELECT * FROM endereco";
            conn = FabricaConexao.getConnection();
            ps = conn.prepareStatement(SQL);
            rs = ps.executeQuery();
            while (rs.next()) {
                endereco = new Endereco();
                endereco.setId(rs.getInt("id"));
                endereco.setLocal(rs.getString("local"));
                endereco.setRua(rs.getString("rua"));
                endereco.setBairro(rs.getString("bairro"));
                endereco.setCidade(rs.getString("cidade"));
                endereco.setEstado(rs.getString("estado"));
                endereco.setCep(rs.getString("cep"));
                endereco.setCapacidadeMaxima(rs.getInt("capacidadeMaxima"));
                enderecos.add(endereco);
            }
        } catch (Exception ex) {
            throw new Exception("Erro ao listar endereco" + ex.getMessage());
        }finally {
            FabricaConexao.closeConnection(conn, ps);
        }
        return enderecos;
    }

    @Override
    public List procurarLocal(String local) throws Exception {
        PreparedStatement ps = null;
        Connection conn = null;
        ResultSet rs;
        Endereco endereco = null;
        List enderecos = new ArrayList();
        try {
            String SQL = "SELECT * FROM endereco WHERE local LIKE ?";
            conn = FabricaConexao.getConnection();
            ps = conn.prepareStatement(SQL);
            ps.setString(1, "%" + local + "%");
            rs = ps.executeQuery();
            while (rs.next()) {
                endereco = new Endereco();
                endereco.setId(rs.getInt("id"));
                endereco.setLocal(rs.getString("local"));
                endereco.setRua(rs.getString("rua"));
                endereco.setBairro(rs.getString("bairro"));
                endereco.setCidade(rs.getString("cidade"));
                endereco.setEstado(rs.getString("estado"));
                endereco.setCep(rs.getString("cep"));
                endereco.setCapacidadeMaxima(rs.getInt("capacidadeMaxima"));
                enderecos.add(endereco);
            }
        } catch (Exception ex) {
            throw new Exception("Erro ao listar endereco" + ex.getMessage());
        }finally {
            FabricaConexao.closeConnection(conn, ps);
        }
        return enderecos;
    }
}
